<?php

namespace App\Http\Controllers;

use App\Models\AdminUsers;
use App\Models\SLessonFeedback;
use App\Models\STaskInfo;
use App\Models\STeacherInfo;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class BaseController extends Controller {
	//
	public function outPutSucc($data = [], $headers = []) {
		$arr["error"] = 0;
		$arr["data"] = $data;
		return response()->json($arr, 200, $headers);
	}

	public function outPutErr($msg = "", $headers = []) {
		$arr["error"] = 1;
		$arr["msg"] = $msg;
		return response()->json($arr, 200, $headers);
	}

	//验证手机号是否合法
	public function prepCheckPhone($phone) {
		$preg = "/^1[345789]\d{9}$/";
		return preg_match($preg, $phone);
	}

	//验证验证码是否正确
	public function checkVerifyCode($phone, $cerifycCde, $key) {

		$saveCode = Cache::get($key);
		// dump($saveCode);
		// dump($cerifycCde);
		if ($saveCode && $saveCode === $cerifycCde) {
			return true;
		} else {
			return false;
		}
	}

	//清除缓存
	public function clearCache($key) {
		Cache::forget('key');
	}

	public function users(Request $request) {
		$q = $request->get('q');

		$list = User::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
		if (!$list->isEmpty()) {
			foreach ($list as $key => $value) {
				$value->text = $value->id . '-' . $value->text;
			}
		}
		return $list;

	}

	public function teachers(Request $request) {
		$q = $request->get('q');

		$list = STeacherInfo::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
		if (!$list->isEmpty()) {
			foreach ($list as $key => $value) {
				$value->text = $value->id . '-' . $value->text;
			}
		}
		return $list;
	}

	public function tasks(Request $request) {
		$q = $request->get('q');

		$list = STaskInfo::where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
		if (!$list->isEmpty()) {
			foreach ($list as $key => $value) {
				$value->text = $value->id . '-' . $value->text;
			}
		}
		return $list;
	}

	public function admins(Request $request) {
		$q = $request->get('q');

		$list = AdminUsers::where('username', 'like', "%$q%")->paginate(null, ['id', 'username as text']);
		if (!$list->isEmpty()) {
			foreach ($list as $key => $value) {
				$value->text = $value->id . '-' . $value->text;
			}
		}
		return $list;
	}

	public function feedbacks(Request $request) {
		$q = $request->get('q');

		$list = SLessonFeedback::where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
		if (!$list->isEmpty()) {
			foreach ($list as $key => $value) {
				$value->text = $value->id . '-' . $value->text;
			}
		}
		return $list;
	}
}
